package com.spmsys.controller.app.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spmsys.service.Auth;
import com.spmsys.service.DatabaseOperation;
import com.spmsys.util.ServletUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

@WebServlet(name = "BatchUpdate", value = "/controller/app/database/BatchUpdate")
public class BatchUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws ServletException, IOException {
        JSONObject requestData;
        JSONObject responseData = new JSONObject();
        PrintWriter printWriter = resp.getWriter();
        HttpSession httpSession = req.getSession();
        try {
            //获取当前时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            //设置字符集、接受所有跨域请求、设置时间
            resp.setContentType("application/json;charset=utf-8");
            resp.setHeader("Access-Control-Allow-Origin", "*");
            resp.setHeader("Access-Control-Allow-Methods", "*");
            resp.setHeader("Access-Control-Allow-Headers", "*");
            resp.setHeader("Date", simpleDateFormat.format(Calendar.getInstance().getTime()));
            //获取请求
            requestData = ServletUtil.getRequestBody(req);
            if (requestData == null) {
                throw new RuntimeException("参数错误");
            }
            //处理请求
            Auth auth = (Auth) httpSession.getAttribute("auth");
            String tableName = requestData.getString("tableName");
            JSONArray where = requestData.getJSONArray("where");
            JSONObject record = requestData.getJSONObject("record");
            if (tableName == null) {
                throw new RuntimeException("tableName为必填参数");
            }
            if (record == null) {
                throw new RuntimeException("record为必填参数");
            }
            JSONObject result = new DatabaseOperation(auth.getUser().getId(), auth.getUser().getId(), auth.getUser().getId()).batchUpdate(tableName, where, record);
            if (result.get("data") == null) {
                throw new RuntimeException(result.getString("errorMessage"));
            }
            //响应请求
            responseData.put("data", result.get("data"));
        } catch (Exception e) {
            responseData.put("errorCode", "DATABASE_BATCH_UPDATE_FAILED");
            responseData.put("errorMessage", e.getMessage());
        }
        printWriter.println(responseData);
        printWriter.close();
    }
}
